package com.crm.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.model.SysUser;
import com.crm.service.IUserService;
import com.crm.util.Courier;

@Controller
public class UserController {

	@Autowired
	private IUserService userService;
	
	//参数工具类
	@Autowired
	private Courier courier;
	
	
	/**
	 * ajax异步判断登陆问题
	 * 1.用户名不存在2.密码错误3.登陆成功
	* @return Courier
	* @param user
	* @return
	 */
	@RequestMapping(value="/login",method=RequestMethod.POST)
	@ResponseBody
	public Courier login(SysUser user,HttpSession session){
		int num = userService.login(user);
		switch (num) {
		case 1:
			courier.setMessage("用户名不存在");
			break;
		case 2:
			courier.setMessage("密码错误");	
			break;
		case 3:
			SysUser sysUser = userService.queryUser(user);
			courier.setMessage("登陆成功");	
			courier.setUsername(sysUser.getUsrName());
			//用户信息存储session
			session.setAttribute("sysUser", sysUser);
			break;
		}	
		return courier;
	}
}
